package com.xuecheng.test.rabbitmq;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

public class service {

    private  static  final  String QUEQUE="helloworld";

    public  static void main(String[] args){
        ConnectionFactory connectionFactory=new ConnectionFactory();
        connectionFactory.setHost("127.0.0.1");
        connectionFactory.setPort(5672);
        connectionFactory.setUsername("guest");
        connectionFactory.setPassword("guest");
        connectionFactory.setVirtualHost("/");
        Connection connection=null;
        try {
            connection=connectionFactory.newConnection();
            Channel channel= connection.createChannel();
            channel.queueDeclare(QUEQUE,true,false,false,null);
            //发送消息
            for (int a=0;a<=100;a++){
                String message="hellow ,你好 ,ID:"+a;
                channel.basicPublish("",QUEQUE,null,message.getBytes());
                System.out.println("发送成功");
            }


            channel.close();
            connection.close();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (TimeoutException e) {
            e.printStackTrace();
        }
    }

}
